<?php
    include("./includes/conf.inc.php");
    if (!isset($_GET['page'])) $page = 1;
    else
    $page = $_GET['page'];
    
    
    if (!isset($_GET['usr'])) $usr = "default";
    else
    $usr = $_GET['usr'];
    
    $id = mysql_connect($db_host,$db_usr,$db_pass) or die("Cannot connect to the DB server: ".mysql_error());
    
    mysql_select_db($db_name, $id);
    
    echo "<form name='UsrForm' action='index.php' method='get'>
    ";
    echo "<input type='hidden' name='page' value='",$page,"' />
    ";  
    echo "<select name='usr' size='1' onchange='UsrForm.submit();'>
    ";
    
    $query = "SELECT username FROM $usr_tbl";
    $res = mysql_query($query, $id);    
    while ($name = mysql_fetch_array($res))
    {
        if ($name[0] == $usr)
        {
          echo "<option value='$name[0]' selected='selected'>$name[0]</option>
          ";
        }
        else
        { 
          echo "<option value='$name[0]'>$name[0]</option>
          ";
        }
    }
    echo "</select>
    ";
    echo "</form>
    ";
    
    $poc_obr = $vyska_alb * $sirka_alb;
    
    $offset = $poc_obr * ($page-1);
    
    $query = "select * from (SELECT $gal_tbl.id, $usr_tbl.username,$gal_tbl.link, $gal_tbl.description FROM $gal_tbl left join $usr_tbl ON $gal_tbl.owner = $usr_tbl.id ORDER BY id DESC) as MyTab where MyTab.username = '$usr' LIMIT $offset, $poc_obr";
    $res = mysql_query($query, $id);
    
    $i = 0;
    $a = array();
    
    echo "<table align='center' cellspacing='10' style='text-align:center; border:0'>
    ";
    $sirkapx = 600 / $sirka_alb;
    while($obj = mysql_fetch_object($res))
    {
        if ($i % $sirka_alb == 0)
        {
        echo "<tr>
        ";
        }
        echo "<td width='$sirkapx'>
        <a href='view.php?imgID=".$obj->id."'><img src='images/gallery/thumbnails/".$obj->link."' border='0' alt='$obj->description' /></a>
        </td>
        ";
              
        $s = $obj->description;
        array_push($a,$s);
        $i++;
        
        if ($i % $sirka_alb == 0)
        {
          echo "</tr>
          ";
          
          echo "<tr>
          ";
          
          foreach ($a as $value)
          {
            echo "<td>".$value." </td>
            ";
          }
          
          echo "</tr>
          ";
          
          $a=array();
        }
    }
    
    if ($i < $sirka_alb - 1)
    { 
      for ($j = $i; $j < $sirka_alb; $j++)
      {
        echo "<td>&nbsp;</td>
        ";
      }
      
      echo "</tr>
      <tr>";
    
    
      foreach ($a as $value)
      {
        echo "<td>".$value." </td>
        ";
      }
      for ($j = $i; $j < $sirka_alb; $j++)
      {
        echo "<td>&nbsp;</td>
        ";
      }
      
      echo "</tr>
      ";
    }
    echo "</table>
    ";


    echo "<center>";
    
    $query ="select * from (SELECT $usr_tbl.username, $gal_tbl.id FROM $gal_tbl left join $usr_tbl ON $gal_tbl.owner = $usr_tbl.id ORDER BY id DESC) as MyTab where MyTab.username = '$usr'";
    $res = mysql_query($query);
    $rows = mysql_num_rows($res);
    for ($i=1; $i<=(($rows/$poc_obr) + 1); $i++)
    {
    	echo "<a href='index.php?page=".$i."&amp;usr=".$usr."'>".$i."</a>&nbsp;";
    }
echo "</center>
";
    mysql_close();
?>